<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>给套接字绑定名字</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.socket-bind" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">socket_bind</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.1.0, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">socket_bind</span> &mdash; <span class="dc-title">给套接字绑定名字</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.socket-bind-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>socket_bind</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$socket</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$address</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$port</code><span class="initializer"> = 0</span></span>
  ] ) : <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   绑定 <code class="parameter">address</code> 到 <code class="parameter">socket</code>。
   该操作必须是在使用 <span class="function"><a href="socket_connect.html" class="function">socket_connect()</a></span> 或者 <span class="function"><a href="socket_listen.html" class="function">socket_listen()</a></span>
   建立一个连接之前。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.socket-bind-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">socket</code></dt>

     <dd>

      <p class="para">
       用 <span class="function"><a href="socket_create.html" class="function">socket_create()</a></span> 创建的一个有效的套接字资源。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">address</code></dt>

     <dd>

      <p class="para">
       如果套接字是 <strong><code>AF_INET</code></strong> 族，那么
       <code class="parameter">address</code> 必须是一个四点分法的 IP 地址（例如 <em>127.0.0.1</em> ）。
      </p>
      <p class="para">
       如果套接字是 <strong><code>AF_UNIX</code></strong> 族，那么 <code class="parameter">address</code> 是 Unix 套接字一部分（例如 <var class="filename">/tmp/my.sock</var> ）。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">port</code> （可选）</dt>

     <dd>

      <p class="para">
       参数 <code class="parameter">port</code> 仅仅用于 <strong><code>AF_INET</code></strong>
       套接字连接的时候，并且指定连接中需要监听的端口号。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.socket-bind-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
  <p class="para">
   错误代码会传入 <span class="function"><a href="socket_last_error.html" class="function">socket_last_error()</a></span> ，如果将此参数传入 <span class="function"><a href="socket_strerror.html" class="function">socket_strerror()</a></span>
   则可以得到错误的文字说明。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.socket-bind-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="snmp.get.example.oidarray">
    <p><strong>Example #1 使用 <span class="function"><strong>socket_bind()</strong></span> 来设置套接字资源地址</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Create&nbsp;a&nbsp;new&nbsp;socket<br /></span><span style="color: #0000BB">$sock&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">socket_create</span><span style="color: #007700">(</span><span style="color: #0000BB">AF_INET</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SOCK_STREAM</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SOL_TCP</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;An&nbsp;example&nbsp;list&nbsp;of&nbsp;IP&nbsp;addresses&nbsp;owned&nbsp;by&nbsp;the&nbsp;computer<br /></span><span style="color: #0000BB">$sourceips</span><span style="color: #007700">[</span><span style="color: #DD0000">'kevin'</span><span style="color: #007700">]&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;</span><span style="color: #DD0000">'127.0.0.1'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$sourceips</span><span style="color: #007700">[</span><span style="color: #DD0000">'madcoder'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">'127.0.0.2'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Bind&nbsp;the&nbsp;source&nbsp;address<br /></span><span style="color: #0000BB">socket_bind</span><span style="color: #007700">(</span><span style="color: #0000BB">$sock</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$sourceips</span><span style="color: #007700">[</span><span style="color: #DD0000">'madcoder'</span><span style="color: #007700">]);<br /><br /></span><span style="color: #FF8000">//&nbsp;Connect&nbsp;to&nbsp;destination&nbsp;address<br /></span><span style="color: #0000BB">socket_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$sock</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'127.0.0.1'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">80</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Write<br /></span><span style="color: #0000BB">$request&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'GET&nbsp;/&nbsp;HTTP/1.1'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\r\n"&nbsp;</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'Host:&nbsp;example.com'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\r\n\r\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">socket_write</span><span style="color: #007700">(</span><span style="color: #0000BB">$sock</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$request</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Close<br /></span><span style="color: #0000BB">socket_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$sock</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.socket-bind-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    该函数必须在 <span class="function"><a href="socket_connect.html" class="function">socket_connect()</a></span> 之前使用。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Windows 9x/ME 兼容性注意点:
    如果尝试绑定套接字资源到一个错误的地址，而这个地址不是本机的地址，那么 <span class="function"><a href="socket_last_error.html" class="function">socket_last_error()</a></span> 可能会返回一个无效的错误代码。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.socket-bind-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="socket_connect.html" class="function" rel="rdfs-seeAlso">socket_connect()</a> - 开启一个套接字连接</span></li>
    <li class="member"><span class="function"><a href="socket_listen.html" class="function" rel="rdfs-seeAlso">socket_listen()</a> - Listens for a connection on a socket</span></li>
    <li class="member"><span class="function"><a href="socket_create.html" class="function" rel="rdfs-seeAlso">socket_create()</a> - 创建一个套接字（通讯节点）</span></li>
    <li class="member"><span class="function"><a href="socket_last_error.html" class="function" rel="rdfs-seeAlso">socket_last_error()</a> - Returns the last error on the socket</span></li>
    <li class="member"><span class="function"><a href="socket_strerror.html" class="function" rel="rdfs-seeAlso">socket_strerror()</a> - Return a string describing a socket error</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>